# Copyright 2023 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load(
    "//cc_toolchain:defs.bzl",
    "ALL_CPP_COMPILER_ACTIONS",
    "ALL_C_COMPILER_ACTIONS",
    "ALL_LINK_ACTIONS",
    "pw_cc_flag_set",
)

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

# Optimization level option
pw_cc_flag_set(
    name = "o2",
    actions = ALL_C_COMPILER_ACTIONS + ALL_CPP_COMPILER_ACTIONS + ALL_LINK_ACTIONS,
    flags = ["-O2"],
)

# Disables linking of the default C++ standard library to allow linking of a
# different version.
pw_cc_flag_set(
    name = "no_default_cpp_stdlib",
    actions = ALL_LINK_ACTIONS,
    flags = ["-nostdlib++"],
)

# Prevent relative paths from being converted to absolute paths.
pw_cc_flag_set(
    name = "no_canonical_prefixes",
    actions = ALL_C_COMPILER_ACTIONS + ALL_CPP_COMPILER_ACTIONS,
    flags = ["-no-canonical-prefixes"],
)

# Compile without runtime type information (RTTI). This produces smaller binaries.
pw_cc_flag_set(
    name = "no_rtti",
    actions = ALL_CPP_COMPILER_ACTIONS,
    flags = ["-fno-rtti"],
)

# Allow uses of the register keyword, which may appear in C headers.
pw_cc_flag_set(
    name = "wno_register",
    actions = ALL_CPP_COMPILER_ACTIONS,
    flags = ["-Wno-register"],
)

# Compile for the C++17 standard.
pw_cc_flag_set(
    name = "c++17",
    actions = ALL_CPP_COMPILER_ACTIONS + ALL_LINK_ACTIONS,
    flags = ["-std=c++17"],
)

# Issue a warning when a class appears to be polymorphic, yet it declares a
# non-virtual destructor
pw_cc_flag_set(
    name = "wnon_virtual_dtor",
    actions = ALL_CPP_COMPILER_ACTIONS,
    flags = ["-Wnon-virtual-dtor"],
)

# Standard compiler flags to reduce output binary size.
pw_cc_flag_set(
    name = "reduced_size",
    actions = ALL_C_COMPILER_ACTIONS + ALL_CPP_COMPILER_ACTIONS,
    flags = [
        "-fno-common",
        "-fno-exceptions",
        "-ffunction-sections",
        "-fdata-sections",
    ],
)

pw_cc_flag_set(
    name = "debugging",
    actions = ALL_C_COMPILER_ACTIONS + ALL_CPP_COMPILER_ACTIONS,
    flags = ["-g"],
)
